define([
    "dojo/_base/declare",
    "gistech/widgets/dialog/Dialog",
    "dojo/_base/lang",
    "dojo/dom-style",
    "dojo/dom-construct",
    "dojo/on",
    "dojo/_base/connect",
    "dojo/dnd/move"
    ],
    function(declare,Dialog,lang,style,domConstruct,on,eventconnect,move){
        return declare([Dialog],{
        	//位置({l:20,t:40})
            pos:null,
            
            mover:null,

            constructor:function (args) {
            	if(args){
            		if (args.pos) {
                        this.pos = args.pos;
                    }
            	}
            },

            postCreate:function () {
                this.mover = new dojo.dnd.Moveable(this.domNode, {handle:this.head});
                eventconnect.connect(this.mover,"onMoveStart",this,this.onMoveStart);
                eventconnect.connect(this.mover,"onMoveStop",this,this.onMoveStop);
                dojo.style(this.head, "cursor", "move");
                dojo.style(this.domNode, "position", "absolute");
                dojo.style(this.domNode, "zIndex", "1001"); //刘方  
                dojo.place(this.domNode, dojo.body());
                this._position();
                this.inherited(arguments);
                var tempThis=this;
                on(this.mover,"Moving",function(mover,lefttop){
                	var window=dojo.window.getBox();
                	var thisbox=dojo.marginBox(tempThis.domNode);
                	lefttop.t=lefttop.t>0?lefttop.t:0;
                	lefttop.l=lefttop.l>0?lefttop.l:0;
                	lefttop.l=(lefttop.l+thisbox.w>window.w-0)?(window.w-0-thisbox.w):lefttop.l;
                	lefttop.t=(lefttop.t+thisbox.h>window.h-0)?(window.h-0-thisbox.h):lefttop.t;
//                	lefttop.t=(lefttop.t>window.h-20)?(window.h-dojo.marginBox(this.head).h):lefttop.t;
                });
            },
            
            //开始拖动
            onMoveStart:function(mover){
            	dojo.style(this.domNode,"opacity",0.7);
            	dojo.addClass(this.domNode,"dialogContainerMoving");
            },
            
            //结束拖动
            onMoveStop:function(mover){
            	dojo.style(this.domNode,"opacity",1);
            	dojo.removeClass(this.domNode,"dialogContainerMoving");
            },

            //定位
            _position:function () {
                if(this.domNode){
                    var thisWidth = this.domNode.clientWidth;
                    var thisHeight = this.domNode.clientHeight;
                    var box = dojo.window.getBox();
                    var left = (box.w - thisWidth) / 2;//默认中心位置
                    var top = (box.h - thisHeight) / 2;

                    if (this.pos) {
                        if (this.pos.l)
                            dojo.style(this.domNode, "left", this.pos.l + "px");
                        else
                            dojo.style(this.domNode, "left", left + "px");
                        if (this.pos.t)
                            dojo.style(this.domNode, "top", this.pos.t + "px");
                        else
                            dojo.style(this.domNode, "top", top + "px");
                    }
                    else {
                        dojo.style(this.domNode, "left", left + "px");
                        dojo.style(this.domNode, "top", top + "px");
                    }
                }
            },
            _setPostion:function(x,y){
            	dojo.style(this.domNode, "left", x + "px");
                dojo.style(this.domNode, "top", y + "px");
            },
            startup:function () {
                this.inherited(arguments);
                this._position();
            }

    });
});